package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.website.dao.WebsiteConfigurationDao;
import com.cyt.website.model.po.WebsiteConfiguration;
import com.cyt.website.model.vo.WebsiteConfigurationVo;
import com.cyt.website.service.WebsiteConfigurationService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.website.util.cache.CacheService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 网站配置管理-网站配置管理Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-13 9:55:10
 */
@Service("websiteConfigurationService")
public class WebsiteConfigurationServiceImpl extends BaseServiceImpl<WebsiteConfiguration> implements WebsiteConfigurationService {

    @Resource
    private CacheService cacheService;
    @Resource
    private SysFileService sysFileService;
    @Resource
    private WebsiteConfigurationDao websiteConfigurationDao;

    @Autowired
    public void setBaseDao(WebsiteConfigurationDao websiteConfigurationDao) {
        this.baseDao = websiteConfigurationDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteConfigurationVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-13 9:55:10
     */
    @Override
    public List<WebsiteConfiguration> listExample(WebsiteConfigurationVo websiteConfigurationVo) {
        Example example = new Example(WebsiteConfiguration.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return websiteConfigurationDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteConfigurationVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:55:10
     */
    @Override
    public ReturnResult page(WebsiteConfigurationVo websiteConfigurationVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteConfiguration> list = listExample(websiteConfigurationVo);
        PageInfo<WebsiteConfiguration> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteConfigurationVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:55:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteConfigurationVo websiteConfigurationVo, SysUser sysUser) {
        if (StrUtil.isEmpty(websiteConfigurationVo.getId())) {
            WebsiteConfiguration websiteConfiguration = new WebsiteConfiguration(sysUser);
            websiteConfiguration.setName(websiteConfigurationVo.getName());
            websiteConfiguration.setPhone(websiteConfigurationVo.getPhone());
            websiteConfiguration.setTelephone(websiteConfigurationVo.getTelephone());
            websiteConfiguration.setLogo(websiteConfigurationVo.getLogo());
            websiteConfiguration.setLogoPath(getFilePath(websiteConfigurationVo.getLogo()));
            websiteConfiguration.setQqCode(websiteConfigurationVo.getQqCode());
            websiteConfiguration.setInteractQr(websiteConfigurationVo.getInteractQr());
            websiteConfiguration.setFollowQr(websiteConfigurationVo.getFollowQr());
            websiteConfiguration.setInteractPath(getFilePath(websiteConfigurationVo.getInteractQr()));
            websiteConfiguration.setFollowPath(getFilePath(websiteConfigurationVo.getFollowQr()));
            websiteConfiguration.setUrl(websiteConfigurationVo.getUrl());
            websiteConfiguration.setCompanyAddress(websiteConfigurationVo.getCompanyAddress());
            websiteConfiguration.setRecordNo(websiteConfigurationVo.getRecordNo());
            websiteConfiguration.setLongitude(websiteConfigurationVo.getLongitude());
            websiteConfiguration.setLatitude(websiteConfigurationVo.getLatitude());
            websiteConfiguration.setDescription(websiteConfigurationVo.getDescription());
            websiteConfiguration.setAboutUs(websiteConfigurationVo.getAboutUs());
            websiteConfiguration.setMailbox(websiteConfigurationVo.getMailbox());
            websiteConfiguration.setStatus(websiteConfigurationVo.getStatus());
            websiteConfiguration.setOrderNum(websiteConfigurationVo.getOrderNum());
            websiteConfiguration.setSeoKeywords(websiteConfigurationVo.getSeoKeywords());
            websiteConfiguration.setSeoDescription(websiteConfigurationVo.getSeoDescription());
            int num = websiteConfigurationDao.insert(websiteConfiguration);
            if (num > 0) {
                cacheService.evictWebsiteConfiguration();
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WebsiteConfiguration websiteConfiguration = websiteConfigurationDao.selectByPrimaryKey(websiteConfigurationVo.getId());
            websiteConfiguration.setName(websiteConfigurationVo.getName());
            websiteConfiguration.setPhone(websiteConfigurationVo.getPhone());
            websiteConfiguration.setTelephone(websiteConfigurationVo.getTelephone());
            websiteConfiguration.setLogo(websiteConfigurationVo.getLogo());
            websiteConfiguration.setLogoPath(getFilePath(websiteConfigurationVo.getLogo()));
            websiteConfiguration.setQqCode(websiteConfigurationVo.getQqCode());
            websiteConfiguration.setInteractQr(websiteConfigurationVo.getInteractQr());
            websiteConfiguration.setFollowQr(websiteConfigurationVo.getFollowQr());
            websiteConfiguration.setInteractPath(getFilePath(websiteConfigurationVo.getInteractQr()));
            websiteConfiguration.setFollowPath(getFilePath(websiteConfigurationVo.getFollowQr()));
            websiteConfiguration.setUrl(websiteConfigurationVo.getUrl());
            websiteConfiguration.setCompanyAddress(websiteConfigurationVo.getCompanyAddress());
            websiteConfiguration.setRecordNo(websiteConfigurationVo.getRecordNo());
            websiteConfiguration.setLongitude(websiteConfigurationVo.getLongitude());
            websiteConfiguration.setLatitude(websiteConfigurationVo.getLatitude());
            websiteConfiguration.setDescription(websiteConfigurationVo.getDescription());
            websiteConfiguration.setAboutUs(websiteConfigurationVo.getAboutUs());
            websiteConfiguration.setMailbox(websiteConfigurationVo.getMailbox());
            websiteConfiguration.setStatus(websiteConfigurationVo.getStatus());
            websiteConfiguration.setOrderNum(websiteConfigurationVo.getOrderNum());
            websiteConfiguration.setSeoKeywords(websiteConfigurationVo.getSeoKeywords());
            websiteConfiguration.setSeoDescription(websiteConfigurationVo.getSeoDescription());
            websiteConfiguration.setUpdateTime(new Date());
            websiteConfiguration.setUpdateUser(sysUser.getId());
            int num = websiteConfigurationDao.updateByPrimaryKey(websiteConfiguration);
            if (num > 0) {
                cacheService.evictWebsiteConfiguration();
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteConfigurationVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:55:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteConfigurationVo websiteConfigurationVo, SysUser sysUser) {
        //要修改的字段
        WebsiteConfiguration websiteConfiguration = new WebsiteConfiguration();
        websiteConfiguration.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        websiteConfiguration.setUpdateUser(sysUser.getId());
        websiteConfiguration.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteConfiguration.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteConfigurationVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteConfigurationVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteConfigurationDao.updateByExampleSelective(websiteConfiguration, example);
        cacheService.evictWebsiteConfiguration();
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteConfigurationVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:55:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteConfigurationVo websiteConfigurationVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteConfiguration.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteConfigurationVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteConfigurationVo.getId().split(",")));
        }
        websiteConfigurationDao.deleteByExample(example);
        cacheService.evictWebsiteConfiguration();
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }
}